const AllModel = require('./models')
const { Hash,compare,sign,verify} = require('./utils/tools')

//用户管理


//注册用户
const signup = async (req,res,next)=>{
    res.set('content-type','application/json; charset=utf-8')
    const { username,password,Tel,role} = req.body
    console.log(req.body);
    //密码加密
    const bcryptPassword = await Hash(password)
    //判断用户是否存在
    let findResult = await AllModel.findUser(username)
    if(findResult){
        res.render('fail',{
            data:JSON.stringify({
                message:"用户存在"
            })
        })
    }else{
        //数据库没有这个用户则添加这个用户
        let result = await AllModel.signup({
            username,
            password:bcryptPassword,
            Tel,
            role,
        })

        res.render('success',{
            data:JSON.stringify({
                message:"用户注册已成功"
            })
        })
    }
}

//用户登录
const login = async(req,res,next)=>{
    const {username,password} = req.body
  
    let result = await AllModel.findUser(username)
    if (result) {
       //验证用户是否为合法用户
        let {password:hash} = result
        let compareResult = await compare(password,hash)
        if (compareResult) {

            const token = sign(username)
            //自定义首部字段向前端传入token
            res.set('X-Access-Token',token)
            res.set('Access-Control-Expose-Headers', 'X-Access-Token')
            res.render('success',{
                data:JSON.stringify({
                    message:"登录成功",
                }),
            })
        } else {
            res.render('fail',{
                data:JSON.stringify({
                    message:"错了笨蛋",
                })
            })
        }
   }else{
        res.render('fail',{
            data:JSON.stringify({
                message:"用户名密码错误",
               
            })
        })
   }
       
}

//获取用户列表
const UserList = async(req,res,next)=>{
    res.set('content-type','application/json; charset=utf-8')
    const listResult = await AllModel.findList()
    res.render('success',{
        data:JSON.stringify(listResult)
    })
}

//模糊搜索
const Grabble = async(req,res,next)=>{
    res.set('content-type','application/json; charset=utf-8')
    const {params} = req.body
    const GrabbleUsername = await AllModel.findGrabbleName(params)
    if (GrabbleUsername) {
        res.render('success',{
            data:JSON.stringify({
                GrabbleUsername,
                message:"查询成功",
            }),
        })
    }else{
        res.render('fail',{
            data:JSON.stringify({
                message:"查询失败",
               
            })
        })
    }
}

//删除用户
const remove = async(req,res,next)=>{
    res.set('content-type','application/json; charset=utf-8')
    const {id} = req.body
    let result =  await AllModel.remove(id)
    if (result) {
        res.render('success',{
            data:JSON.stringify({
                message:'用户删除成功'
            })
        })
        
    } else {
        res.render('fail',{
            data:JSON.stringify({
                message:'用户删除失败'
            })
        })
    }
}

// id查询用户
const ListOne = async (req,res,next)=>{
    let findResult = await AllModel.findOne(req.body.id)
    if (findResult) {
        res.json(findResult)
    }else{
        res.render('fail',{
            findResult,
            data:JSON.stringify({
                message:'用户查找失败'
            })
        })
    }
}
//修改用户
const ModifyUser = async (req,res,next)=>{
    res.set('content-type','application/json; charset=utf-8')
    let {id,username,Tel,role} = req.body
    // id = JSON.stringify(id)
    //判断用户是否存在
        //修改用户
        let result = await AllModel.upDate({
            id,
            username,
            Tel,
            role
        })
        if(result){
            res.render('success',{
                data:JSON.stringify({
                    id,
                    username,
                    message:"用户修改成功"
                })
            })
        }else{
             res.render('fail',{
                data:JSON.stringify({
                    id,
                    username,
                    message:'用户修改失败'
                })
            })
        }
        
       
}



exports.login = login;
exports.UserList = UserList;
exports.signup = signup;
exports.Grabble = Grabble;
exports.remove = remove;
exports.ModifyUser = ModifyUser;
exports.ListOne = ListOne;

//房屋管理

//添加房屋
const SellSignup = async (req,res,next)=>{
    res.set('content-type','application/json; charset=utf-8')
    const {position,resource,area,houseType,state,picture,price} = req.body
    //判断用户是否存在
    let findResult = await AllModel.findSell(resource)
    if(findResult){
        res.render('fail',{
            data:JSON.stringify({
                message:"房屋存在"
            })
        })
    }else{
        //数据库没有这个房屋则添加这个房屋
        let result = await AllModel.SellSignup({
            position,
            resource,
            area,
            houseType,
            state,
            picture,
            price
        })

        res.render('success',{
            data:JSON.stringify({
                message:"房屋添加成功"
            })
        })
    }
}

//获取房屋列表
const SellList = async(req,res,next)=>{
    res.set('content-type','application/json; charset=utf-8')
    const listResult = await AllModel.SellFindList()
    res.render('success',{
        data:JSON.stringify(listResult)
    })
}

//删除房屋
const removeSell = async(req,res,next)=>{
    res.set('content-type','application/json; charset=utf-8')
    const {id} = req.body
    let result =  await AllModel.removeSell(id)
    if (result) {
        res.render('success',{
            data:JSON.stringify({
                result,
                message:'房屋删除成功'
            })
        })
        
    } else {
        res.render('fail',{
            data:JSON.stringify({
                result,
                message:'房屋删除失败'
            })
        })
    }
}

//模糊搜索房屋
const GrabbleSell = async(req,res,next)=>{
    res.set('content-type','application/json; charset=utf-8')
    const {params} = req.body
    console.log(params);
    const GrabbleSell = await AllModel.findGrabbleSell(params)
    if (GrabbleSell) {
        res.render('success',{
            data:JSON.stringify({
                GrabbleSell,
                message:"查询成功",
            }),
        })
    }else{
        res.render('fail',{
            data:JSON.stringify({
                GrabbleSell,
                message:"查询失败",
               
            })
        })
    }
}


// id查询用户
const ListOneSell = async (req,res,next)=>{
    let findResult = await AllModel.findOneSell(req.body.id)
    if (findResult) {
        res.json(findResult)
    }else{
        res.render('fail',{
            findResult,
            data:JSON.stringify({
                message:'用户查找失败'
            })
        })
    }
}
exports.SellList = SellList;
exports.SellSignup = SellSignup;
exports.removeSell = removeSell;
exports.GrabbleSell = GrabbleSell;
exports.ListOneSell = ListOneSell;


//评论管理

//添加评论
const CommentSignup = async (req,res,next)=>{
    res.set('content-type','application/json; charset=utf-8')
    const {date,commentUsers,content,nice} = req.body
    //判断用户是否存在
    let findResult = await AllModel.findComment(date)
    if(findResult){
        res.render('fail',{
            data:JSON.stringify({
                message:"评论存在"
            })
        })
    }else{
        //数据库没有这条评论
        let result = await AllModel.signupComment({
            date,
            commentUsers,
            content,
            nice,
        })

        res.render('success',{
            data:JSON.stringify({
                message:"评论添加成功"
            })
        })
    }
}

//获取评论列表
const CommentList = async(req,res,next)=>{
    res.set('content-type','application/json; charset=utf-8')
    const listResult = await AllModel.CommentFindList()
    res.render('success',{
        data:JSON.stringify(listResult)
    })
}

//模糊搜索评论
const GrabbleComment = async(req,res,next)=>{
    res.set('content-type','application/json; charset=utf-8')
    const {params} = req.body
    const GrabbleComment = await AllModel.findGrabbleComment(params)
    if (GrabbleComment) {
        res.render('success',{
            data:JSON.stringify({
                GrabbleComment,
                message:"查询成功",
            }),
        })
    }else{
        res.render('fail',{
            data:JSON.stringify({
                GrabbleComment,
                message:"查询失败",
               
            })
        })
    }
}

//删除评论
const removeComment = async(req,res,next)=>{
    res.set('content-type','application/json; charset=utf-8')
    const {id} = req.body
    let result =  await AllModel.removeComment(id)
    if (result) {
        res.render('success',{
            data:JSON.stringify({
                result,
                message:'房屋删除成功'
            })
        })
        
    } else {
        res.render('fail',{
            data:JSON.stringify({
                result,
                message:'房屋删除失败'
            })
        })
    }
}

// id查询用户
const ListOneComment = async (req,res,next)=>{
    let findResult = await AllModel.findOneComment(req.body.id)
    if (findResult) {
        res.json(findResult)
    }else{
        res.render('fail',{
            findResult,
            data:JSON.stringify({
                message:'用户查找失败'
            })
        })
    }
}
exports.CommentSignup = CommentSignup
exports.CommentList = CommentList
exports.GrabbleComment = GrabbleComment
exports.removeComment = removeComment
exports.ListOneComment = ListOneComment

const os = require('os');

const getCPU = async(req,res,next)=>{
    const valueCPU = parseInt(os.freemem()/1024/1024)
    if (valueCPU) {
        res.render('success',{
            data:JSON.stringify({
                valueCPU,
                message:'成功'
            })
        })
    } else {
        res.render('fail',{
            data:JSON.stringify({
                valueCPU,
                message:'失败'
            })
        })
    }
}

exports.getCPU = getCPU


//房屋管理

//添加房屋
const HouseSignup = async (req,res,next)=>{
    res.set('content-type','application/json; charset=utf-8')
    const {position,resource,area,houseType,picture,price,state} = req.body
    //判断用户是否存在
    let findResult = await AllModel.findHouse(resource)
    if(findResult){
        res.render('fail',{
            data:JSON.stringify({
                message:"房屋存在",
            })
        })
    }else{
        //数据库没有这个房屋则添加这个房屋
        let result = await AllModel.HouseSignup({
            position,
            resource,
            area,
            houseType,
            state,
            picture,
            price
        })

        res.render('success',{
            data:JSON.stringify({
                result,
                message:"房屋添加成功"
            })
        })
    }
}

//获取房屋列表
const HouseList = async(req,res,next)=>{
    res.set('content-type','application/json; charset=utf-8')
    const listResult = await AllModel.HouseFindList()
    res.render('success',{
        data:JSON.stringify(listResult)
    })
}

//删除房屋
const removeHouse = async(req,res,next)=>{
    res.set('content-type','application/json; charset=utf-8')
    const {id} = req.body
    let result =  await AllModel.removeHouse(id)
    if (result) {
        res.render('success',{
            data:JSON.stringify({
                result,
                message:'房屋删除成功'
            })
        })
        
    } else {
        res.render('fail',{
            data:JSON.stringify({
                result,
                message:'房屋删除失败'
            })
        })
    }
}

//模糊搜索房屋
const GrabbleHouse = async(req,res,next)=>{
    res.set('content-type','application/json; charset=utf-8')
    const {params} = req.body
    console.log(params);
    const GrabbleHouse = await AllModel.findGrabbleHouse(params)
    if (GrabbleHouse) {
        res.render('success',{
            data:JSON.stringify({
                GrabbleHouse,
                message:"查询成功",
            }),
        })
    }else{
        res.render('fail',{
            data:JSON.stringify({
                GrabbleHouse,
                message:"查询失败",
               
            })
        })
    }
}

//修改房屋
const ModifyHouse = async (req,res,next)=>{
    res.set('content-type','application/json; charset=utf-8')
    let {id,position,resource,area,houseType,state,picture,price} = req.body
    // id = JSON.stringify(id)
    //判断用户是否存在
        //修改用户
        let result = await AllModel.upDateHouse({
            id,
            position,
            resource,
            area,
            houseType,
            state,
            picture,
            price
        })
        if(result){
            res.render('success',{
                data:JSON.stringify({
                    id,
                    state,
                    message:"用户修改成功"
                })
            })
        }else{
             res.render('fail',{
                data:JSON.stringify({
                    id,
                    state,
                    message:'用户修改失败'
                })
            })
        }
        
       
}

// id查询用户
const ListOneHouse = async (req,res,next)=>{
    let findResult = await AllModel.findOneHouse(req.body.id)
    if (findResult) {
        res.json(findResult)
    }else{
        res.render('fail',{
            findResult,
            data:JSON.stringify({
                message:'用户查找失败'
            })
        })
    }
}
exports.HouseList = HouseList;
exports.HouseSignup = HouseSignup;
exports.removeHouse = removeHouse;
exports.GrabbleHouse = GrabbleHouse;
exports.ModifyHouse = ModifyHouse;
exports.ListOneHouse = ListOneHouse;
